package com.zhu.transform.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author yingzhi zhu
 *         date 2017/11/15.
 */
public class Md5Util {


    public static String encryptToMd5String(String content,String appKey) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return encryptToMd5String(
                appKey+content);
    }

    public static String encryptToMd5String(String content) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String md5String = null;
        MessageDigest md = null;
        md = MessageDigest.getInstance("md5");
        md.update(content.getBytes("UTF-8"));
        md5String = parseByte2HexString(md.digest());
        return md5String;
    }

    public static String parseByte2HexString(byte buf[])
    {
        StringBuilder stringBuffer = new StringBuilder();
        for (int i = 0; i < buf.length; i++)
        {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {hex = '0' + hex;}
            stringBuffer.append(hex.toUpperCase());
        }
        return stringBuffer.toString();
    }

}
